package com.port.eleport.service.plat.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.port.eleport.dao.BaseDaoI;
import com.port.eleport.dto.base.PageFilter;
import com.port.eleport.dto.plat.Merchant;
import com.port.eleport.dto.sys.Dictionary;
import com.port.eleport.dto.sys.Role;
import com.port.eleport.model.plat.TMerchant;
import com.port.eleport.model.sys.TRole;
import com.port.eleport.service.plat.MerchantServiceI;

@Service
public class MerchantServiceImpl implements MerchantServiceI{

	@Autowired
	private BaseDaoI<TMerchant> merchantDao;
	

	
	public List<Merchant> dataGrid(Merchant merchant, PageFilter ph) {
		// TODO Auto-generated method stub
		List<Merchant> ul = new ArrayList<Merchant>();
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = " from TMerchant t ";
		List<TMerchant> l = merchantDao.find(hql + whereHql(merchant, params) + orderHql(ph), params, ph.getPage(), ph.getRows());
		for (TMerchant t : l) {
			Merchant u = new Merchant();
			BeanUtils.copyProperties(t, u);
			if(t.getType()!=null){
				u.setMerchantTypeId(t.getType().getId());
				u.setMerchantTypeName(t.getType().getName());
			}
			ul.add(u);
		}
		return ul;
	}

	private String whereHql(Merchant merchant, Map<String, Object> params) {
		String hql = "";
		if (merchant != null) {
			hql += " where 1=1 ";
			if (merchant.getCompanyName() != null) {
				hql += " and t.companyName like :companyName";
				params.put("companyName", "%%" + merchant.getCompanyName() + "%%");
			}
			if(merchant.getMerchantTypeId()!=null){
				hql += " and t.type.id = :merchantTypeId";
				params.put("merchantTypeId", merchant.getMerchantTypeId());
			}
		}
		return hql;
	}

	private String orderHql(PageFilter ph) {
		String orderString = "";
		if ((ph.getSort() != null) && (ph.getOrder() != null)) {
			orderString = " order by t." + ph.getSort() + " " + ph.getOrder();
		}
		return orderString;
	}
	
	public Long count(Merchant merchant, PageFilter ph) {
		// TODO Auto-generated method stub
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = " from TMerchant t ";
		return merchantDao.count("select count(*) " + hql + whereHql(merchant, params), params);
	}

	public void add(Merchant merchant) {
		// TODO Auto-generated method stub
		
	}

	public void delete(String id) {
		// TODO Auto-generated method stub
		
	}

	public void edit(Merchant merchant) {
		// TODO Auto-generated method stub
		
	}

	public Merchant get(String id) {
		// TODO Auto-generated method stub
		TMerchant t = merchantDao.get(TMerchant.class, id);
		Merchant m = new Merchant();
		BeanUtils.copyProperties(t, m);
		if(t.getType()!=null){
			m.setMerchantTypeId(t.getType().getId());
			m.setMerchantTypeName(t.getType().getName());
		}
		return m;
	}

	
}
